<template>
    <div>
        <div>
            <el-input
                    class="addJobIntput"
                    size="small"
                    style=""
                    placeholder="请输入职称"

                    prefix-icon="el-icon-plus"
                    v-model="jl.name">
            </el-input>
            <el-select class="addJobSelect" size="small" v-model="jl.titleLevel" placeholder="请选择职称级别">
                <el-option
                        v-for="item in titlelevels"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-button icon="el-icon-plus" style="" size="small" type="primary" @click="addJob">添加</el-button>
        </div>

        <div class="jobLeveManaTable">
            <el-table
                    :data="jobLData"
                    @selection-change="handleCheckedCitiesChange"
                    border
                    stripe
                    size="small"
                    style="width: 70%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称级别"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button size="small" @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button size="small" type="danger" style="margin-top: 8px;" :disabled="multipleSelection.length == 0" @click="deleteAll">批量删除</el-button>
        </div>
        <el-dialog
                title="修改职位"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td><el-tag>职称名称</el-tag></td>
                        <td><el-input class="updatePosInput" size="small" v-model="updateJl.name"></el-input></td>
                    </tr>
                    <tr>
                        <td><el-tag>职称等级</el-tag></td>
                        <td>
                            <el-select size="small" v-model="updateJl.titleLevel" placeholder="请选择活动区域">
                                <el-option
                                        v-for="item in titlelevels"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td><el-tag>是否启用</el-tag></td>
                        <td>
                            <el-switch
                                    v-model="updateJl.enabled"
                                    active-text="启用"
                                    inactive-text="禁用">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogVisible = false">取 消</el-button>
            <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
          </span>
        </el-dialog>
    </div>

</template>

<script>
    //import EditJobLevel from "./EditJobLevel";
    export default {
        name: "JobLevelMana",
        //components: {EditJobLevel},
        data() {
            return {
                jl: {
                    name:'',
                    titleLevel:''
                },
                dialogVisible:false,
                updateJl:{
                    name:'',
                    titleLevel:'',
                    enabled:false
                },
                titlelevels: [{
                    value: '正高级',
                    label: '正高级'
                }, {
                    value: '副高级',
                    label: '副高级'
                }, {
                    value: '中级',
                    label: '中级'
                }, {
                    value: '初级',
                    label: '初级'
                }, {
                    value: '员级',
                    label: '员级'
                }],
                jobLData:[],
                multipleSelection:[]
            }
        },
        mounted() {
            this.innitJob();
        },
        methods:{
            //初始化查询所有数据
            innitJob() {
                this.getRequest("/system/basic/joblevel/").then(resp => {
                    if (resp) {
                        this.jobLData = resp.obj;
                    }
                });
            },
            //单条删除
            handleDelete(index,row) {
                this.$confirm('此操作将永久删除该"'+row.name+'"职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                                                                //get请求
                    this.deleteRequest("/system/basic/joblevel/" + row.id).then(resp =>{
                        if (resp) {
                            this.innitJob();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //点击选择框，回调选中
            handleCheckedCitiesChange(value) {
                this.multipleSelection = value;
                console.log(value);
            },
            //批量删除
            deleteAll() {
                this.$confirm('此操作将永久删除该【'+this.multipleSelection.length+'】条, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item =>{
                        ids += 'ids='+item.id +'&';
                    });
                    this.deleteRequest("/system/basic/joblevel/" +ids).then(resp =>{
                        if (resp) {
                            this.innitJob();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //点击编辑
            showEditView(index,row) {
                this.dialogVisible = true;
                Object.assign(this.updateJl,row);
            },
            doUpdate() {
                this.putRequest("/system/basic/joblevel/",this.updateJl).then(resp =>{
                    if (resp) {
                        this.innitJob();
                        this.dialogVisible = false;
                    }
                })
            },
            //新增
            addJob() {
                if (this.jl.name || this.jl.titleLevel) {
                                                                //post请求 参数
                    this.postRequest("/system/basic/joblevel/",this.jl).then(resp =>{
                        if (resp) {
                            this.jl.name = '';
                            this.jl.titleLevel = '';
                            this.innitJob();
                        }
                    });
                } else {
                    if (!this.jl.name) {
                        this.$message.error("职称名称不能为空")
                    }
                    if (!this.jl.titleLevel) {
                        this.$message.error("职称级别不能为空")
                    }
                }
            }
            //

        }
    }
</script>

<style scoped>

    .addJobIntput {
        width: 300px;
        margin-right: 10px
    }
    .addJobSelect {
        margin-right: 10px
    }
    .jobLeveManaTable {
        margin-top: 10px;
    }
</style>
